package com.echoplots.json.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.echoplots.json.entity.Student;
import com.echoplots.json.entity.Teacher;
import com.echoplots.json.entity.User;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class UserController {
	@RequestMapping("getuser")
	@ResponseBody
	public User getUser() {
	    User user = new User();
	    user.setUserName("EchoPlots");
	    user.setBirthday(new Date());
	    return user;
	}
	
	
	
	
	@Autowired
	ObjectMapper mapper;

	//序列化
	@RequestMapping("serialization")
	@ResponseBody
	public String serialization() {
	    try {
	        User user = new User();
	        user.setUserName("EchoPlots");
	        user.setBirthday(new Date());
	        //Jackson通过使用mapper的writeValueAsString方法将Java对象序列化为JSON格式字符串
	        String str = mapper.writeValueAsString(user);
	        return str;
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return null;
	}
	
	
	//readTree方法可以接受一个字符串或者字节数组、文件、InputStream等， 返回JsonNode作为根节点，你可以像操作XML DOM那样操作遍历JsonNode以获取数据
	@RequestMapping("readjsonstring")
	@ResponseBody
	public String readJsonString() {
	    try {
	        String json = "{\"name\":\"EchoPlots\",\"age\":26}";
	        JsonNode node = this.mapper.readTree(json);
	        String name = node.get("name").asText();
	        int age = node.get("age").asInt();
	        return name + " " + age;
	        
	        
	        //解析多级JSON
/*	        String json = "{\"name\":\"EchoPlots\",\"hobby\":{\"first\":\"sleep\",\"second\":\"eat\"}}";;
	        JsonNode node = this.mapper.readTree(json);
	        JsonNode hobby = node.get("hobby");
	        String first = hobby.get("first").asText();*/
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return null;
	}
	
	//绑定对象 将Java对象和JSON数据进行绑定
	@RequestMapping("readjsonasobject")
	@ResponseBody
	public String readJsonAsObject() {
	    try {
	        String json = "{\"name\":\"EchoPlots\",\"age\":26}";
	        User user = mapper.readValue(json, User.class);
	        String name = user.getUserName();
	        int age = user.getAge();
	        return name + " " + age;
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return null;
	}
	
	
	
	@RequestMapping("getstudent")
	@ResponseBody
	public Student getstudent() {
	    Student student = new Student();
	    student.setUserName("EchoPlots");
	    student.setBirthday(new Date());
	    return student;
	}
	
	
	
	
	@RequestMapping("readjsonasobject")
	@ResponseBody
	public String readJsonAsObject1() {
	    try {
	        String json = "{\"user-name\":\"EchoPlots\"}";
	        Student user = mapper.readValue(json, Student.class);
	        String name = user.getUserName();
	        return name;
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return null;
	}
	
	
	
	@JsonView(Teacher.TeacherNameView.class)
	@RequestMapping("getteacher")
	@ResponseBody
	public Teacher getTeacher() {
		Teacher teacher = new Teacher();
	    teacher.setUserName("EchoPlots");
	    teacher.setAge(26);
	    teacher.setPassword("123456");
	    teacher.setBirthday(new Date());
	    return teacher;
	}
	
	
	//这里的List<User>泛型不会被檫除
	//[{"userName":"EchoPlots","age":26},{"userName":"scott","age":27}]
	@RequestMapping("updateuser")
	@ResponseBody
	public int updateUser(@RequestBody List<User> list){
	    return list.size();
	}
	
	
	//List.class泛型被檫除  java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.example.pojo.User
	@RequestMapping("customize")
	@ResponseBody
	public String customize() throws JsonParseException, JsonMappingException, IOException {
	    String jsonStr = "[{\"userName\":\"EchoPlots\",\"age\":26},{\"userName\":\"scott\",\"age\":27}]";
	    List<User> list = mapper.readValue(jsonStr, List.class);
	    String msg = "";
	    for (User user : list) {
	        msg += user.getUserName();
	    }
	    return msg;
	}
	
	
	//JavaType ，用来指明集合类型
	@RequestMapping("customize1")
	@ResponseBody
	public String customize1() throws JsonParseException, JsonMappingException, IOException {
	    String jsonStr = "[{\"userName\":\"EchoPlots\",\"age\":26},{\"userName\":\"scott\",\"age\":27}]";
	    JavaType type = mapper.getTypeFactory().constructParametricType(List.class, User.class);
	    List<User> list = mapper.readValue(jsonStr, type);
	    String msg = "";
	    for (User user : list) {
	        msg += user.getUserName();
	    }
	    return msg;
	}
}
